var app = getApp()
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 创建一个日程表
function setNewCalendar (date) {
  var that = this
  var timeList = []
  timeList.push({
    startTime: new Date(),
    duration: '',
    endTime: '',
    schedule: ''
  })
  if (date == null) {
    date = new Date()
  }
  app.globalData.calendarList.push({
    date: date,
    timeList: timeList
  })
}

// 添加一条日程
function setNewTimeList(list) {
  if (list.length != 0 && list[list.length - 1].endTime) {
    var date = list[list.length - 1].endTime
  } else {
    var date = this.getTime(new Date())
  }
  return {
    startTime: date,
    duration: '0',
    endTime: date,
    schedule: ''
  }
}

function updateNewTimeList(timeList, keyword) {
  timeList = keyword
}

// 获取统一当前时间
function getTime (date) {
  const hour = date.getHours()
  const minute = date.getMinutes()
  return [hour, minute].map(formatNumber).join(':')
}

// 获得统一日期
function getDate (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
// 获得X天后的日期
function getLastDate(addDateCount) {
  var date = new Date()
  date.setDate(date.getDate() + addDateCount)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
// 计算时间差值
function getDValueOfTime (timeList) {
  var that = this

  var startTimeArray = timeList.startTime.split(':')
  var endTimeArray = timeList.endTime.split(':')

  var startTimeHour = parseInt(startTimeArray[0])
  var startTimeMinute = parseInt(startTimeArray[1])

  var endTimeHour = parseInt(endTimeArray[0])
  var endTimeMinute = parseInt(endTimeArray[1])

  if (endTimeMinute < startTimeMinute) {
    endTimeMinute = endTimeMinute + 60
    endTimeHour = endTimeHour - 1
  }
  return (endTimeHour - startTimeHour) * 60 + (endTimeMinute - startTimeMinute)
}
// 统计时间之和
function getSumOfTime (data, key) {
  var sumOfTime = 0
  data.forEach(function (index,item) {
    if (item.schedule == key) {
      sumOfTime += parseInt(item.duration)
    }
  })
  return sumOfTime
}
// 报错弹窗
function failShow (str) {
  wx.showToast({
    title: str,
    icon: 'fail',
    duration: 1000,
    mask: true
  })
}
// 成功弹窗
function successShow(str) {
  wx.showToast({
    title: str,
    icon: 'success',
    duration: 1000,
    mask: true
  })
}
// 将信息保存到本地
/**
 * 可能保存的信息包括：
 * 每天的日程key = 日期例如：5.30
 * 事情列表 key = calendarList
 * 
 */
function saveInfo(key, data) {
  wx.setStorage({
    key: key,
    data: data,
  })
  this.successShow('保存成功')
}
function getInfo(key) {
  var list = wx.getStorageSync(key)
  if (list) {
    return wx.getStorageSync(key)
  } else {
    return []
  }
  
  // wx.getStorage({
  //   key: key,
  //   success: function (res) {
  //     console.log('res',res.data)
  //     list =  res.data
  //   },
  //   complete: function (res) {
  //     return list
  //   }
  // })
}
// 将信息日程保存到全局变量中
function saveInfoToApp (data) {
  var appSet = new Set(app.globalData.calendarList)
  appSet.add(data)
}
/**
 * 下面进行日志相关方法的封装，
 * 包括选择时间段、
 * 统计事情及其时间、
 */
function getTimeSlotByIndex (index) {
  switch(index){
    case '0':
      return 1
    case '1':
      return 7
      
    case '2':
      return 30
    case '3':
      return 90
    case '4':
      return 182
    case '5':
      return 365
  }
}
function getLogs (index) {
  var daycount = getTimeSlotByIndex(index)
  var logMap = new Map()
  var totalTime = 0
  var date = getDate(new Date())
  for (var i = 0; i < daycount; i++) {
    var calendar = getInfo(date)
    calendar.forEach(function (item,index) {
      var duration = parseInt(item.duration)
      totalTime = totalTime + duration
      if (logMap.get(item.schedule)) {
        duration = duration + parseInt(logMap.get(item.schedule))
      }
      logMap.set(item.schedule,duration)
      date = getLastDate(-i - 1)
    })
  }
  var logs = []
  logMap.forEach(function (value,key,map) {
    logs.push({
      schedule: key,
      duration: value,
      percentage: ((value / totalTime) * 100).toFixed(2) + '%' 
    })
  })
  return logs
}
// // 将日期改为统一格式
// function changeListItem (listItem) {
//   listItem.startShow = this.getTime(listItem.startTime)
//   listItem.endShow = this.getTime(listItem.endTime)
//   listItem.durationShow = this.getTime(listItem.duration)
//   return listItem
// }

// function changeList (list) {
//   var that = this
//   list.forEach(function(item, index) {
//     item = that.changeListItem
//   })
//   console.log(list)
//   return list
// }
module.exports = {
  formatTime: formatTime,
  setNewCalendar: setNewCalendar,
  setNewTimeList: setNewTimeList,
  getTime: getTime,
  getDate: getDate,
  getLastDate: getLastDate,
  getDValueOfTime: getDValueOfTime,
  failShow: failShow,
  successShow: successShow,
  saveInfo: saveInfo,
  saveInfoToApp: saveInfoToApp,
  getSumOfTime: getSumOfTime,
  getInfo: getInfo,
  getTimeSlotByIndex: getTimeSlotByIndex,
  getLogs: getLogs
  // changeListItem: changeListItem,
  // changeList: changeList
}
